/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.losalpes.catalogo.beans;

/**
 *
 * @author da.joya49
 */
import com.losalpes.catalogo.bos.CarItem;
import com.losalpes.catalogo.bos.Mueble;
import com.losalpes.catalogo.delegate.TiendaLosAlpesBussinesDelegate;
import com.losalpes.catalogo.servicios.ICatalogService;
import com.sun.facelets.tag.jstl.core.ForEachHandler;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;

public class StoreBean {

    private CarItem carItem = new CarItem();
    private List<CarItem> carItems = new ArrayList<CarItem>();

    private ICatalogService catalogService;
    
    private Mueble mueble = new Mueble();


    /** Creates a new instance of StoreBean */
    public StoreBean() {
    }
    
    public CarItem getCarItem() {
        return carItem;
    }

    public void setCarItem(CarItem carItem) {
        this.carItem = carItem;
    }

    public ICatalogService getCatalogService() {
        return catalogService;
    }

    public void setCatalogService(ICatalogService catalogService) {
        this.catalogService = catalogService;
    }

    public Mueble getMueble() {
        return mueble;
    }

    public void setMueble(Mueble mueble) {
        this.mueble = mueble;
    }

    /**
     *
     * @return
     */
    public List<CarItem> getCarItems() {
        try {
            carItems = TiendaLosAlpesBussinesDelegate.getInstance().getCarItems();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return carItems;
    }

    public void setCarItems(List<CarItem> carItems) {
        this.carItems = carItems;
    }

    public void add(CarItem carItem) {

        carItem.getMueble().setNombre(mueble.getNombre());
        
        try {
            TiendaLosAlpesBussinesDelegate.getInstance().addCarItem(carItem);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SelectItem[] getMuebles() {

        List<Mueble> mueblesList = catalogService.findAllMuebles();

        SelectItem[] sitems = new SelectItem[mueblesList.size()];

        int i = 0;
        for(Mueble mueble: mueblesList) {
            sitems[i] = new SelectItem(mueble.getReferencia(), mueble.getDescripcion());
        }

        return sitems;
    }
}
